using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using UnityEngine.Video;
using UnityEditor;

public class UIDisplay : MonoBehaviour
{
    // Start is called before the first frame update
    public Canvas Canvas;
    public InputActionReference menu;
    public VideoPlayer video;
    private bool wasPlayingBeforeDisable = false;

    private void OnEnable()
    {
        if(wasPlayingBeforeDisable && video != null)
        {
            video.Play();
        }
    }
    void Start()
    {
        if(menu != null)
        {
            menu.action.Enable();
            menu.action.performed += ToggleMenu;
        }
    }

    private void ToggleMenu(InputAction.CallbackContext context)
    {
        Canvas.enabled = !Canvas.enabled;
    }

    // Update is called once per frame
    private void OnDisable()
    {
        if(video != null)
        {
            wasPlayingBeforeDisable = video.isPlaying;
            if (wasPlayingBeforeDisable)
            {
                video.Pause();
            }
        }
    }
    public void OnCanvasVisibilityChanged(bool isVisible)
    {
        if (isVisible)
        {
            if (wasPlayingBeforeDisable)
            {
                video.Play();
            }
        }
        else
        {
            wasPlayingBeforeDisable = video.isPlaying;
            if (wasPlayingBeforeDisable)
            {
                video.Pause();
            }
        }
    }
}
